/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pack;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;
import net.useobjects.AbstractMovableRotatableGroup;
import net.useobjects.Circle;
import net.useobjects.geom.Position;

public class Ciel extends PohyblivyObjekt {

    private Circle ciel;
    private ArrayList<Kamen> zoznamKamenov;
    private ArrayList<Strela> zoznamStriel;
    private ArrayList<Ciel> zoznamCielov;
    private final int posun = 5;
    private double angle;
    private Random rand;

    public Ciel(AbstractMovableRotatableGroup parent, Position pozicia, ArrayList<Kamen> zoznamKamenov, ArrayList zoznamStriel,ArrayList<Ciel> zoznamCielov) {
        super(parent, pozicia);
        this.zoznamKamenov = zoznamKamenov;
        this.zoznamStriel = zoznamStriel;
        this.zoznamCielov = zoznamCielov;
        
        rand = new Random();
        ciel = new Circle(this, 0, 0, 10, Color.green);
        angle = generateAngle();
        ciel.setRotation(angle);
    }

    private double generateAngle() {
        double a;
        a = rand.nextInt(360);
        return Math.toRadians(a);
    }
    
    //implementacia zakona odrazu
    private double otoc(double rotation) {
        
        //I. kvadrant
        if( (Math.signum(Math.sin(rotation)) > 0)  && (Math.signum(Math.cos(rotation)) > 0) ) {
            return 2*Math.PI-rotation;
        }
        
        //II. kvadrant
        if( (Math.signum(Math.sin(rotation)) > 0)  && (Math.signum(Math.cos(rotation)) < 0)  ) {
            return Math.PI-rotation;
        }
        
        //III. kvadrant
        if( (Math.signum(Math.sin(rotation)) < 0)  && (Math.signum(Math.cos(rotation)) < 0)  ) {
            return 2*Math.PI-rotation;
        }
        
        //IV. kvadrant
        if( (Math.signum(Math.sin(rotation)) < 0) && (Math.signum(Math.cos(rotation)) > 0)  ) {
            return Math.PI-rotation;
        }
        return 2*Math.PI-rotation;
    }
    
    @Override
    protected void process() {
        
        if (mozemSaPohnut(ciel, zoznamKamenov)  && jeVoSvete(ciel)) {
            moveInDirection(posun, ciel.getRotation());
        } 
        
        else {
            angle = otoc(ciel.getRotation());
            ciel.setRotation(angle);
            moveInDirection(posun, ciel.getRotation());
        }

//        if (trafilCiel(this, zoznamStriel)) {
        if(zasah()) {
            timer.stop();
//            ciel.setColor(Color.yellow);
            ciel.setVisible(false);
            ciel = null;
            zoznamCielov.remove(this);
//            System.out.println(zoznamCielov.size());
            
            
            
        }
    }
    
    private boolean zasah() {
        
        for(Strela s: zoznamStriel) {
            if( getPolarRadius( transformLocalCoordinates(s.getPositionInRootGroup(),this.getPositionInRootGroup() ) ) <=10 )
                return true;
        }
        
        return false;
    }
}
